package com.yunxi.module.system.mapper.tenant;

import com.yunxi.framework.common.pojo.PageResult;
import com.yunxi.framework.mybatis.core.mapper.BaseMapperX;
import com.yunxi.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.yunxi.module.system.model.tenant.TenantDO;
import com.yunxi.module.system.vo.admin.tenant.tenant.TenantPageReqVO;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * <p>
 * 租户 mapper
 * </p>
 *
 * @author lidy
 * @since 2024-05-08
 */
@Mapper
public interface TenantMapper extends BaseMapperX<TenantDO> {

    /**
     * 获得名字对应的租户
     *
     * @param name 租户名
     * @return 租户
     */
    default TenantDO selectByName(String name) {
        return selectOne(TenantDO::getName, name);
    }

    /**
     * 获得租户分页列表
     *
     * @param reqVO 条件
     * @return 租户分页列表
     */
    default PageResult<TenantDO> selectPage(TenantPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<TenantDO>()
                .likeIfPresent(TenantDO::getName, reqVO.getName())
                .likeIfPresent(TenantDO::getContactName, reqVO.getContactName())
                .likeIfPresent(TenantDO::getContactMobile, reqVO.getContactMobile())
                .eqIfPresent(TenantDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(TenantDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(TenantDO::getId));
    }

    /**
     * 根据租户域名查询
     *
     * @param website 租户域名
     * @return 租户信息
     */
    default TenantDO selectByWebsite(String website) {
        return selectOne(TenantDO::getWebsite, website);
    }


    /**
     * 获得使用指定套餐的租户数组
     *
     * @param packageId 租户套餐编号
     * @return 租户数组
     */
    default List<TenantDO> selectListByPackageId(Long packageId) {
        return selectList(TenantDO::getPackageId, packageId);
    }

    /**
     * 根据租户套餐编号查询租户数量
     *
     * @param packageId 租户套餐编号
     * @return 租户数量
     */
    default Long selectCountByPackageId(Long packageId) {
        return selectCount(TenantDO::getPackageId, packageId);
    }
}
